%% Identification
% David Krause
% Line Follower 2012
%
%% Code
% Work with measured ADC data versus distance to determine start/finish
% line indication

%% Clean up
clc
close all
clear all
drawnow

%% Data versus position
pos = (-15 : 5 : 30);
adc = (0 : 5);


adc_data = [9 8 175 135 12 8;
            11 7 137 192 52 8; 
            127 8 10 174 131 11;
            180 28 8 135 207 61; 
            209 112 9 9 176 184; 
            216 195 15 8 113 188;
            218 215 129 11 10 163;
            187 190 181 46 8 36;
            182 221 224 179 43 8;
            179 212 214 196 60 8;
            219 228 230 225 231 211;
            182 174 188 201 211 193; 
            72 83 167 212 199 53
            9 7 43 199 215 59
            10 7 120 179 43 7
            9 7 9 176 126 9
            172 192 49 7 8 7
            28 7 65 192 35 8];

seq = [1 -1 -1 1 1 -1; ...
       1.25 0.0 -1.5 0 1.25 0.0; ...
       1 1 -1 -1 1 1];

% 1 0 0 1 1 0; ... 
%        1 0 0 1 1 -0.6; ...
%        1 -0.3, -0.3, 1, 1, -0.3; ...
%        1 -1, -1, 1, 1, -1];
   
%        1, 0.5, -0.25, 0.5, 1.25, 0.5; ...
%        1 1 0 0 1 1];

% For each reading, calculate the correlation coefficients
corr_found = zeros(length(pos), size(seq, 1));
for ii = 1 : size(adc_data, 1)
    temp = sum((adc_data(ii, :) / 4) .^ 2);
    % Approximate the square root
    adc_norm = (temp / 225 + 48) / 4;
    disp(adc_norm)
    
    
    for jj = 1 : size(seq, 1)        
        corr_found(ii, jj) = sum(adc_data(ii, :) .* seq(jj, :)) * 8 / adc_norm;
    end
end


figure
plot(corr_found)
